package com.swz.dcrm.ui.car;


import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingScaleTabLayout;
import com.swz.dcrm.adpter.CarModelTagAdapter;
import com.swz.dcrm.adpter.FragmentViewPagerAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarPkFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tab)
    SlidingScaleTabLayout slidingTabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_right)
    TextView tvRight;
    List<Fragment>fragments;
    String[] titles;

    @Inject
    CarManagementViewModel carManagementViewModel;
    public static CarPkFragment newInstance() {

        return new CarPkFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_car_pk;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        titles=getResources().getStringArray(R.array.car_pk_tab);
        Drawable drawable=getResources().getDrawable(R.mipmap.edit_tag);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        tvRight.setCompoundDrawables(drawable, null, null, null);

        fragments=new ArrayList<>(2);
        fragments.add(CarCompareFragment.newInstance());
        fragments.add(CarConfigFragment.newInstance());
        viewPager.setAdapter(new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, Arrays.asList(titles)));
        viewPager.setOffscreenPageLimit(2);
        slidingTabLayout.setViewPager(viewPager);
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_right)
    public void edit(){
        if(TextUtils.isEmpty(tvRight.getText().toString())){
            tvRight.setCompoundDrawables(null, null, null, null);
            tvRight.setText(getString(R.string.finish));
            carManagementViewModel.getEdit().setValue(true);
        }else {
            Drawable drawable=getResources().getDrawable(R.mipmap.edit_tag);
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            tvRight.setCompoundDrawables(drawable, null, null, null);
            tvRight.setText("");
            carManagementViewModel.getEdit().setValue(false);
        }
    }
}
